/*
 * @Author: 何善万 swhe@che300.com
 * @Date: 2024-08-16 14:32:34
 * @LastEditors: 何善万 swhe@che300.com
 * @LastEditTime: 2024-08-20 16:39:27
 * @FilePath: \my-notion-redux\src\redux\slices\global.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createSlice } from '@reduxjs/toolkit';
import {storageData} from '@tools/utils';
import {LANGUAGE_TYPE} from '@tools/constants'

const globalSlice = createSlice({
  name: 'globalSlice',
  initialState: {
    isLoading: false,
    isShowMenu: false,
    theme: '',
    language: storageData(LANGUAGE_TYPE) || 'zh'
  },
  reducers: {
    changLoading: (state, { payload }) => {
      state.isLoading = payload;
    },
    setGlobalCommon: (state, { payload }) => {
      if (Object.prototype.toString.call(payload) === '[object Object]' && Object.keys(payload).length > 0) {
        for (const item in payload) {
          state[item] = payload[item];
        }
      } else {
        throw Error('payload must be an object!');
      }
    }
  }
});

export const { changLoading, setGlobalCommon } = globalSlice.actions;
export default globalSlice.reducer;
